#!/usr/bin/env python
# -*- coding: utf-8 -*-


import sys
#import ctypes
import os
import ConfigParser

#CONFIG='/etc/osiriseye.cfg'
CONFIG='./osiriseye.cfg'

class Ag_Motion:


	def __init__(self, name, framerate, numrows, numcols, color_BN, directori,gap,fr_pre_capture,threshold,start_event_script,end_event_script,pid,port,alias,duration):
		# Constructor
		self.name = name;
		self.fr = framerate
		self.rows = numrows
		self.cols = numcols
		self.color_BN = color_BN
		self.directori = directori
		self.gap = gap
		self.fr_pre_capture = fr_pre_capture
		self.threshold = int(threshold*numrows*numcols)
		self.start_event_script = start_event_script
		self.end_event_script = end_event_script
		self.alias = alias
		self.port = port
		self.pid = pid
		self.duration=duration
		self.config_path=''
		self.recording = False
		import motion_lib
		#self.am = ctypes.CDLL('./am.so')
		self.am = motion_lib

		# Posem valors per defecte a la camera

		self.am.AM_Set_NewCam(self.name,self.fr,self.rows,self.cols,self.color_BN,self.directori,self.gap,self.fr_pre_capture,self.threshold ,self.start_event_script,self.end_event_script,self.pid,self.port,self.alias,self.duration)   


	# dona el numero de cameres del sistema
	def Consulta_Num_Cams(self):
		self.am.AM_Get_NumCams()

	# modifica el valor del fr rate rang [2..100], 100 mean no SW limit, limited then by HW
	def Modificar_FrameRate(self,fr):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.fr=fr
			self.am.AM_Set_FrameRate(self.name,self.fr)
			return True
	
	# modifica el nombre de files [1..255]
	def Modificar_Num_Rows(self,rows):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:	
			self.rows = rows
			self.am.AM_Set_Rows(self.name, self.rows)
			return True

	# modifica el nombre de colunes, dependent del dispostiu multiple de 16 (640, 480, 352, 320, 288, 240)
	def Modificar_Num_Cols(self, cols):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:	
			self.cols = cols
			self.am.AM_Set_Cols(self.name, self.cols)
			return True

	# tipus == 1 :: Color, tipus == 0:: Blanc i negre
	def Modificar_BN_Color(self,tipus):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.Color_BN = tipus
			self.am.AM_Set_Color_BN(self.name, self.Color_BN) 
			return True

	# modifica el directori desti de les dades
	def Modificar_Directori(self,direct):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.directori = direct
			self.am.AM_Set_Directory(self.name, self.directori)
			return True

	# modifica el temps de grabacio despres de l'ultima deteccio de moviment
	# temps en segons
	def Modificar_TPostBuff(self,temps):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.gap = temps
			self.am.AM_Set_GapMotion(self.name,self.gap)
			return True

	# modifica el temps de grabacio abans de la deteccio de moviment
	# temps en segons
	def Modificar_TPreBuff(self,temps):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.fr_pre_capture=temps
			self.am.AM_Set_Frame_precapture(self.name,self.fr_pre_capture)
			return True

	# sensibilitat del metode de deteccio [0, 0.1, 0.2....1] (0 ==> han de canviar tots)
	def Modicar_Sensibilitat(self,sensibilitat):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.threshold = sensibilitat
			if self.threshold == 0.0: self.am.AM_Set_Threshold(self.name, self.threshold)
			else: self.am.AM_Set_Threshold(self.name, int(self.threshold*(self.rows*self.cols)))
			return True

	# modifica la localitzacio del script que s'executara quan es detecti un moviment
	def Modificar_Start_Script(self,full_path):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.start_event_script = full_path
			self.am.AM_Set_start_event_script(self.name, self.start_event_script)
			return True

	# modifica la localitzacio del script que s'executara quan es detecti fi d'un moviment
	def Modificar_End_Script(self,full_path):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.start_event_script = full_path
			self.am.AM_Set_end_event_script(self.name, self.start_event_script)
			return True

	def Modificar_Alies(self,alies):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.alias = alies
			self.am.AM_Set_Modificar_Alies(self.name, self.alias)
			return True

	def Modificar_PID(self,pid):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.pid = pid
			self.am.AM_Set_Modificar_PID(self.name,self.pid)
			return True

	def Modificar_Port(self,port):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.port = port
			self.am.AM_Set_Modificar_port(self.name, self.port)
			return True

	def Modificar_Max_Duration(self,duration):
		if (self.recording):
			print "Ag_Motion::Don't change when recording!"
			return False
		else:
			self.duration = duration
			self.am.AM_Set_Max_Video_Duration(self.name, self.duration)
			return True

	# genera fitxer de configuracio del motion
	# name ==> device
	def Generar_MotionFile(self, path_name):
		self.config_path = path_name
		self.am.AM_Create_MotionFile(self.config_path,self.name)

	def Reconfigurar_Motion(self):
		os.system('kill -s HUP ' + self.pid)

	# Genera el fitxer de configuracio del motion i fa que el motion rellegeixi 
	# el fitxer de configuracio (el daemon del motion ha d'estar creat anteriorment)
	def Activacio_Motion(self):
		self.am.AM_Create_MotionFile(self.config_path,self.name)
		os.system('kill -s HUP ' + self.pid)

	# Per defecte la gravacio continua esta OFF
	def Inici_Gravacio_continua(self):
		self.am.AM_Inici_Gravacio_continua(self.name)

	def Fi_Gravacio_continua(self):
		self.am.AM_Fi_Gravacio_continua(self.name)

	# Per defecte la deteccio del moviment esta ON
	def Activar_Deteccio(self):
		self.am.AM_Activar_Deteccio(self.name)

	def Desactivar_Deteccio(self):
		self.am.AM_Desactivar_Deteccio(self.name)
		
	def setRecording(self,value):
		self.recording = value
    
def main(argv):
	#(self, name, framerate, numrows, numcols, color_BN, directori,gap,fr_pre_capture,threshold,start_event_script,end_event_script,pid,port,alias,duration)

	A = Ag_Motion('/dev/video0',0.2,320,240,1,'/tmp',60,60,0.1,'/tmp/on_start','/tmp/on_end',900,8000,'alias',120)

	A.Generar_MotionFile('/tmp/defecte_thres.out' )
	
	A.Inici_Gravacio_continua()
	A.Desactivar_Deteccio()
	A.Generar_MotionFile('/tmp/gravacio_on_deccio_off.out' )

	A.Fi_Gravacio_continua()
	A.Desactivar_Deteccio()
	A.Generar_MotionFile('/tmp/gravacio_off_deccio_off.out' )

	A.Activar_Deteccio()
	A.Inici_Gravacio_continua()
	A.Generar_MotionFile('/tmp/gravacio_on_deccio_on.out' )

	A.Activar_Deteccio()
	A.Fi_Gravacio_continua()
	A.Generar_MotionFile('/tmp/gravacio_off_deccio_on.out' )

if __name__ == '__main__': main(sys.argv)
